package person.aoao.library.service.impl;

import person.aoao.library.dao.BookListDao;
import person.aoao.library.entity.TypeList;
import person.aoao.library.dao.TypeListDao;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.FloorListService;
import person.aoao.library.service.TypeListService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TypeList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:40
 */
@Service("typeListService")
public class TypeListServiceImpl implements TypeListService {
    @Resource
    private TypeListDao typeListDao;
    @Resource
    private FloorListService floorListService;

    @Resource
    private BookListDao bookListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param typeid 主键
     * @return 实例对象
     */
    @Override
    public TypeList queryById(Integer typeid) {
        return this.typeListDao.queryById(typeid);
    }

    /**
     * 分页查询
     *
     * @param typeList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<TypeList> queryByPage(TypeList typeList, PageRequest pageRequest) {
        long total = this.typeListDao.count(typeList);
        return new PageImpl<>(this.typeListDao.queryAllByLimit(typeList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param typeList 实例对象
     * @return 实例对象
     */
    @Override
    public TypeList insert(TypeList typeList) {
        this.typeListDao.insert(typeList);
        return typeList;
    }

    /**
     * 修改数据
     *
     * @param typeList 实例对象
     * @return 实例对象
     */
    @Override
    public TypeList update(TypeList typeList) {
        this.typeListDao.update(typeList);
        return this.queryById(typeList.getTypeid());
    }

    /**
     * 通过主键删除数据
     *
     * @param typeid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer typeid) {
        return this.typeListDao.deleteById(typeid) > 0;
    }

    @Override
    public ResponseData getTypeList(Integer num, Integer page) {
        ListData data=new ListData();
        List<TypeList> list=this.typeListDao.getTypeList(num,(page-1)*num);

        for(TypeList item:list){
            item.setFloorList(this.floorListService.queryById(item.getFid()));
            item.setBookCount(this.bookListDao.getTypeCount(item.getTypeid()));
        }
        long count=this.typeListDao.getCount(num,(page-1)*num);
        data.setCount(count);
        data.setData(list);
        return  new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData searchTypeList(Integer fid, Integer num, Integer page) {
        ListData data=new ListData();
        List<TypeList> list=this.typeListDao.searchTypeList(fid,num,(page-1)*num);
        for(TypeList item:list){
            item.setFloorList(this.floorListService.queryById(item.getFid()));
            item.setBookCount(this.bookListDao.getTypeCount(item.getTypeid()));
        }
        long count=this.typeListDao.getSearchCount(fid,num,(page-1)*num);
        data.setCount(count);
        data.setData(list);
        return  new ResponseData(ResponseCode.SUCCESS,data);
    }
}
